<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul {
            display: flex;
            list-style: none;
        }

        li {
            margin: 10px 20px;
        }
    </style>
</head>

<body>
    <!-- s使用hash模式原理 -->
    <ul>
        <li>
            <span onclick="go({'name':'home'},'','home')">首页</span>
        </li>
        <li>
            <span onclick="go({'name':'category'},'','category')">分类</span>
        </li>
        <li>
            <span onclick="go({'name':'car'},'','car')">购物车</span>
        </li>
        <li>
            <span onclick="go({'name':'mine'},'','mine')">我的</span>
        </li>
    </ul>

    <router-view id="box"></router-view>
</body>

</html>
<script>
    let box = document.querySelector('#box')
    // 使用hashchange 事件监听hash值的改变
    function go(a, b, c) {
        // 向当前的历史记录中添加路由信息
        history.pushState(a, b, c)
        if (a.name == 'home') {
            box.innerHTML = 'home'
        }
        if (a.name == 'category') {
            box.innerHTML = '分类'
        }
        if (a.name == 'car') {
            box.innerHTML = '购物车'
        }
        if (a.name == 'mine') {
            box.innerHTML = '我的'
        }
    }

    // 监听浏览器的历史栈的变化
    window.onpopstate = (e) => {
        //  当切换历史记录的时候,触发该popstate方法,监听到当前路由的信息e.state,然后根据该信息,显示对应的组件页面
        console.log(e);
        if (e.state.name == 'home') {
            box.innerHTML = '首页'
        }
        if (e.state.name == 'category') {
            box.innerHTML = '分类'
        }
        if (e.state.name == 'car') {
            box.innerHTML = '购物车'
        }
        if (e.state.name == 'mine') {
            box.innerHTML = '我的'
        }
    }


</script>